﻿<UserControl x:Class="Frontend.LoginPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:Frontend"
    Name="RootControl"
    DataContext="{Binding RelativeSource={x:Static RelativeSource.Self}}">
    <UserControl.Resources>
        <Style x:Key="ItemStyle">
            <Setter Property="Control.Padding" Value="1" />
            <Setter Property="Control.Margin" Value="1" />            
        </Style>        
        <Style x:Key="ErrorItemStyle"  BasedOn="{StaticResource ItemStyle}">
            <Setter Property="Border.BorderThickness" Value="1" />
        </Style>
        <src:RefreshStatusToVisibilityConverter x:Key="RefreshStatusToVisibilityConverter" />
        <src:RefreshStatusToBoolConverter       x:Key="RefreshStatusToBoolConverter"       />
        <src:EmptyStringToParameterConverter    x:Key="EmptyStringToParameterConverter"    />       
        <SolidColorBrush x:Key="ErrorBackgroundBrush" Color="Pink" />
        <SolidColorBrush x:Key="ErrorBorderBrush"    Color="Red"  />
    </UserControl.Resources>
    
    <Expander Name="Expander" IsExpanded="True">
        <Expander.Header>
            <DockPanel>
                <TextBlock Margin="0 0 5 0">Login</TextBlock>
                
                <!-- StackPanel which displays additional info when control is collapsed. -->
                <StackPanel VerticalAlignment="Center">
                    <StackPanel.Visibility>
                        <Binding ElementName="Expander" Path="IsExpanded" 
                                 Converter="{StaticResource BoolToVisibilityConverter}"
                                 ConverterParameter="{StaticResource NotConverter}" />
                    </StackPanel.Visibility>
                    
                    <TextBlock>
                        <TextBlock.Visibility>
                            <Binding Path="RefreshStatus"
                                     Converter="{StaticResource RefreshStatusToVisibilityConverter}"
                                     ConverterParameter="{x:Static src:RefreshStatus.NoUser}" />
                        </TextBlock.Visibility>
                        <TextBlock.Inlines>
                        ( 
                        <Hyperlink Click="NoUserClicked" Foreground="Red">
                            User is empty
                        </Hyperlink> 
                        )
                        </TextBlock.Inlines>
                    </TextBlock>                    
                    
                    <TextBlock>
                        <TextBlock.Visibility>
                            <Binding Path="RefreshStatus"
                                     Converter="{StaticResource RefreshStatusToVisibilityConverter}"
                                     ConverterParameter="{x:Static src:RefreshStatus.NoPassword}" />
                        </TextBlock.Visibility>
                        <TextBlock.Inlines>
                        ( 
                        <Hyperlink Click="NoPasswordClicked" Foreground="Red">
                            Password is empty
                        </Hyperlink>                    
                        )
                        </TextBlock.Inlines>
                    </TextBlock>
                    
                    <TextBlock >
                         <TextBlock.Visibility>
                            <Binding Path="RefreshStatus"
                                     Converter="{StaticResource RefreshStatusToVisibilityConverter}"
                                     ConverterParameter="{x:Static src:RefreshStatus.CanRefresh}" />
                        </TextBlock.Visibility>
                        <TextBlock.Inlines>
                        ( 
                        <Hyperlink Click="RefreshClicked">
                            Refresh
                        </Hyperlink>                    
                        )
                        </TextBlock.Inlines>
                    </TextBlock>
                    
                    <TextBlock >
                        <TextBlock.Visibility>
                            <Binding Path="RefreshStatus"
                                     Converter="{StaticResource RefreshStatusToVisibilityConverter}"
                                     ConverterParameter="{x:Static src:RefreshStatus.Refreshing}" />
                        </TextBlock.Visibility>
                        <TextBlock.Inlines>
                        ( 
                        <Run Foreground="Green">
                            Refreshing...
                        </Run> 
                        )
                        </TextBlock.Inlines>
                    </TextBlock>
                    
                    <TextBlock >
                        <TextBlock.Visibility>
                            <Binding Path="RefreshStatus"
                                     Converter="{StaticResource RefreshStatusToVisibilityConverter}"
                                     ConverterParameter="{x:Static src:RefreshStatus.CannotRefresh}" />
                        </TextBlock.Visibility>
                        <TextBlock.Inlines>
                        ( 
                        <Run Foreground="Red">
                        Can't Refresh 
                        </Run>
                        )
                        </TextBlock.Inlines>
                    </TextBlock>
                </StackPanel>
            </DockPanel>
        </Expander.Header>
        <GroupBox Padding="0 5 0 0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.5*" />
                    <ColumnDefinition Width="0.5*" />
                </Grid.ColumnDefinitions>
                
                <Label Grid.Row="0" Grid.Column="0"
                       Style="{StaticResource ItemStyle}"
                       Content="User" />
                
                <Label Grid.Row="1" Grid.Column="0"
                       Style="{StaticResource ItemStyle}"
                       Content="Password" />


                <Border Grid.Row="0" Grid.Column="1" 
                        Style="{StaticResource ErrorItemStyle}" >
                    <Border.BorderBrush >
                        <Binding Path="User"                                                                  
                                     Converter="{StaticResource EmptyStringToParameterConverter}"
                                     ConverterParameter="{StaticResource ErrorBorderBrush}" />
                    </Border.BorderBrush>
                    <TextBox  Name="UserTextBox"                                
                              >

                        <TextBox.Text>
                            <Binding Path="User" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                        </TextBox.Text>
                        <TextBox.IsEnabled>
                            <Binding Path="IsRefreshing" Converter="{StaticResource NotConverter}" />
                        </TextBox.IsEnabled>
                        <!--TextBox.Background>
                            <Binding Path="User" 
                                     Converter="{StaticResource EmptyStringToParameterConverter}"
                                     ConverterParameter="{StaticResource ErrorBackgroundBrush}" />
                        </TextBox.Background-->
                    </TextBox>
                </Border>

                <Border Grid.Row="1" Grid.Column="1" 
                        Style="{StaticResource ErrorItemStyle}" >
                    <Border.BorderBrush>
                        <Binding Path="Password"                                                                  
                                     Converter="{StaticResource EmptyStringToParameterConverter}"
                                     ConverterParameter="{StaticResource ErrorBorderBrush}" />
                    </Border.BorderBrush>
                    <PasswordBox    Name="PasswordTextBox"
                                    PasswordChanged="PasswordControlChanged">
                        
                        <PasswordBox.IsEnabled>
                            <Binding Path="IsRefreshing" Converter="{StaticResource NotConverter}" />
                        </PasswordBox.IsEnabled>
                        <!--PasswordBox.Background>
                            <Binding Path="Password"                                                                  
                                     Converter="{StaticResource EmptyStringToParameterConverter}"
                                     ConverterParameter="{StaticResource ErrorBackgroundBrush}" />
                        </PasswordBox.Background-->
                    </PasswordBox>
                </Border>
               
                <ComboBox       Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
                                Name="SchoolComboBox" Style="{StaticResource ItemStyle}" 
                                Padding="2" HorizontalContentAlignment="Center" SelectedIndex="0" 
                                SelectionChanged="SchoolControlChanged">
                    <ComboBox.IsEnabled>
                        <Binding ElementName="RootControl" Path="IsRefreshing" Converter="{StaticResource NotConverter}" />
                    </ComboBox.IsEnabled>
                    <ComboBox.Items>
                        <ComboBoxItem Content="Kyiv, Livoberezhna"   Tag="{x:Static src:SchoolName.KyivLivoberezhna}"   />
                        <ComboBoxItem Content="Kyiv, Universytet"    Tag="{x:Static src:SchoolName.KyivUniversytet}"    />
                        <ComboBoxItem Content="Kyiv, Zhytomyrska"    Tag="{x:Static src:SchoolName.KyivZhytomyrska}"    />
                        <ComboBoxItem Content="Kyiv, Poshtova"       Tag="{x:Static src:SchoolName.KyivPoshtova}"       />
                        <ComboBoxItem Content="Kyiv, Pecherska"      Tag="{x:Static src:SchoolName.KyivPecherska}"      />
                        <ComboBoxItem Content="Odesa, Rishelevskaya" Tag="{x:Static src:SchoolName.OdesaRishelevskaya }"/>
                    </ComboBox.Items>                    
                </ComboBox>
                
                <Button         Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"                                  
                                Style="{StaticResource ItemStyle}" Click="RefreshClicked" >
                    
                    <Button.Content>
                        Refresh
                    </Button.Content>
                    <Button.IsEnabled>
                        <Binding ElementName="RootControl" Path="RefreshStatus"
                                 Converter="{StaticResource RefreshStatusToBoolConverter}" 
                                 ConverterParameter="{x:Static src:RefreshStatus.CanRefresh}" />                                 
                    </Button.IsEnabled>
              </Button>
                
            </Grid>
        </GroupBox>
    </Expander>
</UserControl>
